home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-07-11 | 4.5 KB | 186 lines | [TEXT/MPCC] |
- /*
- ** UTurboTCP.cp
- **
- ** TurboTCP support library
- ** Utility functions
- **
- ** Copyright © 1993-94, FrostByte Design / Eric Scouten
- **
- */
-
- #include "UTurboTCP.h"
-
- #include "CTCPDriver.h"
- #include "CTCPResolverCall.h"
-
-
- extern Boolean gInBackground; // TCL global: in background under MultiFinder
-
-
- // —— opening/closing TCP services ——
-
- /*______________________________________________________________________
- **
- ** InitTCP (static method)
- **
- ** Create a TCP driver object and initialize it. Should be called once at application startup.
- ** In a future version of TurboTCP, this routine will check for MacTCP and/or Open Transport
- ** and open the appropriate object.
- **
- */
-
- void UTurboTCP::InitTCP()
-
- {
- if (!CTCPDriver::gTCPDriver)
- new CTCPDriver;
- }
-
-
- /*______________________________________________________________________
- **
- ** CloseTCP (static method)
- **
- ** Close the TCP driver object and destroy it. Should be called once at application
- ** shut-down time.
- **
- */
-
- void UTurboTCP::CloseTCP()
-
- {
- if (CTCPDriver::gTCPDriver)
- (CTCPDriver::gTCPDriver)->Dispose();
- }
-
-
- // —— processing network events ——
-
- /*______________________________________________________________________
- **
- ** ProcessNetEvents (static method)
- **
- ** Call once at every event-loop (or frequent non-interrupt-level) to handle processing
- ** of TCP completions and notifications.
- **
- ** maxEventsToProcess (short): maximum number of TCP events to process this time
- ** maxTicks (long): maximum ticks to spend in this loop
- ** positive values: use value “as is”
- ** zero: use intelligent default
- ** negative value: use value with compensation
- ** for background activities
- */
-
- void UTurboTCP::ProcessNetEvents(short maxEventsToProcess, long maxTicks)
-
- {
- static long lastTickCount = 0; // tick count last time through routine
- long stopTickCount; // tick count to stop processing events
- long defaultNetTicks; // how long are we typically in net event loop
- long defaultNetWait; // how long to delay before each net event loop
-
-
- // if no TCP driver, quit now
-
- if (!CTCPDriver::gTCPDriver)
- return;
-
-
- // decide how long we can go through net event loop
-
- if (maxEventsToProcess < 1)
- maxEventsToProcess = 100;
- if (lastTickCount == 0)
- lastTickCount = LMGetTicks();
-
- if (!gInBackground) {
- defaultNetTicks = 45; // spend up to 3/4 second on net events
- defaultNetWait = 0; // if in foreground
- }
- else {
- defaultNetTicks = 40; // consume less time in background
- defaultNetWait = 3; // and give other apps more chance to
- // do meaningful work
- }
-
-
- // adjust time in net event loop to other CPU activity
-
- if (maxTicks < 1) {
- long elapsedTicks = LMGetTicks() - lastTickCount; // long time, no see?
-
- if (elapsedTicks < defaultNetWait) // ensure that other apps can do meaningful
- return; // stuff before we hog the CPU again
-
- if (maxTicks < 0) // negative maxTicks values will override
- defaultNetTicks = -maxTicks; // earlier decision of how much time
-
- maxTicks = defaultNetTicks - elapsedTicks; // reduce time if CPU is otherwise busy
-
- if (maxTicks < 2) // make sure we get at least a little time
- maxTicks = 2;
- if (maxTicks > defaultNetTicks) // not sure how this would happen, but …
- maxTicks = defaultNetTicks;
- }
-
-
- // record the current time
-
- lastTickCount = LMGetTicks();
- stopTickCount = lastTickCount + maxTicks;
-
-
- // loop through async event queue (until queue is empty or time is up)
-
- while ((maxEventsToProcess-- > 0) && (LMGetTicks() < stopTickCount)) {
- if (!(CTCPDriver::gTCPDriver)->ProcessOneNetEvent())
- break; // no more events to process
- }
-
-
- // rerecord current time (in case net event processing was incredibly slow)
-
- lastTickCount = LMGetTicks();
-
- }
-
-
- // —— IP address utilities ——
-
- /*______________________________________________________________________
- **
- ** GetLocalIPAddr (static method)
- **
- ** Get the local machine’s IP address.
- **
- ** return (unsigned long): the local IP address
- **
- */
-
- unsigned long UTurboTCP::GetLocalIPAddr()
-
- {
- if (CTCPDriver::gTCPDriver)
- return (CTCPDriver::gTCPDriver)->GetIPAddr();
- else
- return 0L;
- }
-
-
- /*______________________________________________________________________
- **
- ** DoAddrToStr (static method)
- **
- ** Convert an IP address to dotted decimal format.
- **
- ** theIPaddr (unsigned long): the IP address to convert
- ** theString (char*): pointer to a buffer for the string (16 chars)
- **
- */
-
- void UTurboTCP::DoAddrToStr(unsigned long theIPaddr, char* theString)
-
- {
- CTCPResolverCall::DoAddrToStr((ip_addr) theIPaddr, theString);
- }
-